using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CSharp.Basic.Learn.并发多线程
{
  /// <summary>
  /// Parallel Linq
  /// </summary>
  public class PEnumerable
  {
    public PEnumerable()
    {
    }

    /// <summary>
    /// 非线程安全
    /// </summary>
    public  void ListWithParallel()
    {
      List<int> list = new List<int>();
      Parallel.For(0, 10000, item =>
      {
        list.Add(item);
      });
      Console.WriteLine("List's count is {0}", list.Count());
    }

    /// <summary>
    /// 线程安全
    /// </summary>
    public  void ConcurrentBagWithPallel()
    {
      ConcurrentBag<int> list = new ConcurrentBag<int>();
      Parallel.For(0, 10000, item =>
      {
        list.Add(item);
      });
      Console.WriteLine("ConcurrentBag's count is {0}", list.Count());
    }
  }
}
